package com.example.demo.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @author: Bin.L
 * @date: 2021/4/9 09:51
 * @Description:
 */
@Service
@Slf4j
public class EmailListener {

    @EventListener
    @Async
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)
    public void onApplicationEvent(MessageEvent messageEvent) {
        log.info(" {} send Email start. . .  ", Thread.currentThread().getName());
        try {
            // 模拟发送调用耗时
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.warn(" Email send process, send message: {}", messageEvent.getMessage());
        log.info(" Email send complete. . . ");
    }

}
